<?php
namespace app\admin\controller;

use app\common\component\admin\AdminService;
use app\common\component\admin\AdminValidate;

class Admin extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();
	}

	public function index() {
		$this -> view -> assign('admins', (new AdminService) -> getAdmins());
		return $this -> view -> fetch();
	}

	public function create() {
		if($this -> request -> isPost()) {
			$post = $this -> request -> post();
			
			$resultAdminValidate = (new AdminValidate) -> create($post);
			if($resultAdminValidate['code'] === 0) {
				unset($post['comfirm_password']);

				$resultAdminCreate = (new AdminService) -> create($post);
				if($resultAdminCreate['code'] === 0) {
					echo '<meta charset="utf-8"><script>alert("管理员创建成功!");window.location.href="/shuashuale/index";</script>';exit;
				} else {
					$this -> error($resultAdminCreate['message']);
				}
			} else {
				echo '<meta charset="utf-8"><script>alert("' . $resultAdminValidate['message'] . '!");</script>';				
			}
		}

		return $this -> view -> fetch();
	}

	public function ban() {
		$get = $this -> request -> get();
		$resultAdminUpdate = (new AdminService()) -> update(['status' => $get['status']], $get['admin_id']);
		if($resultAdminUpdate['code'] === 0) {
			echo '<meta charset="utf-8"><script>alert("' . $resultAdminUpdate['message'] . '");window.location.href="/shuashuale/index";</script>';exit;
		} else {
			$this -> error($resultAdminUpdate['message']);
		}
	}

	public function update() {
		$adminService = new AdminService();
		if($this -> request -> isPost()) {
			$post = $this -> request -> post();
			if(empty($post['password'])) {
				unset($post['password']);
			} else {
				$post['password'] = getPasswordHash($post['password']);
			}

			$resultAdminUpdate = $adminService -> update($post, $this -> request -> get('admin_id'));
			if($resultAdminUpdate['code'] === 0) {
				echo '<meta charset="utf-8"><script>alert("' . $resultAdminUpdate['message'] . '");window.location.href="/shuashuale/index";</script>';exit;
			} else {
				$this -> error($resultAdminUpdate['message']);
			}
		}

		$resultAdmin = $adminService -> getAdmin($this -> request -> get('admin_id'));
		$this -> view -> assign('admin', $resultAdmin['data']['admin']);

		return $this -> view -> fetch();
	}

	public function delete() {
		$resultAdmin = (new AdminService()) -> delete($this -> request -> get('admin_id'));

		if($resultAdmin['code'] === 0) {
			echo '<meta charset="utf-8"><script>alert("' . $resultAdmin['message'] . '");window.location.href="/shuashuale/index";</script>';exit;
		} else {
			$this -> error($resultAdmin['message']);
		}
	}

	public function changePassword() {
		if($this -> request -> isPost()) {
			$post = $this -> request -> post();

			$resultAdminValidate = (new AdminValidate) -> checkPassword($post);
			if($resultAdminValidate['code'] === 0) {
				$save['password'] = getPasswordHash($post['new_password']);

				$resultAdminUpdate = (new AdminService) -> update($save);
				if($resultAdminUpdate['code'] === 0) {
					echo '<meta charset="utf-8"><script>alert("修改成功!");window.location.href="/shuashuale/change_password";</script>';exit;
				} else {
					$this -> error($resultAdminUpdate['message']);
				}
			} else {
				echo '<meta charset="utf-8"><script>alert("' . $resultAdminValidate['message'] . '!");</script>';				
			}

			
		}

		return $this -> view -> fetch();
	}
}